﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) [INCLUDE APACHE 2.0 AS AN OPTION.]
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.PerformancePoint.Scorecards;
using Microsoft.PerformancePoint.Scorecards.DesignerPlugins.Deploy.SharePoint;
using Microsoft.PerformancePoint.Scorecards.DesignerWorkspace;
using Microsoft.PerformancePoint.Scorecards.DesignerPlugins.Deploy;
using ppscd.Configuration;

namespace ppscd
{
    /// <summary>
    /// Dashboard deployer
    /// </summary>
    public class DashboardDeployer
    {
        public const string V4MasterPagePath = "/_catalogs/masterpage/v4.master";

        /// <summary>
        /// Master Page Path
        /// Ex. /BICenter/_catalogs/masterpage/v4.master
        /// </summary>
        public string MasterPagePath
        {
            get;
            set;
        }

        /// <summary>
        /// Deploy a dasboard. The ScorecardModel instance first needs to be loaded with
        /// any elements contained on the Dashboard.
        /// </summary>
        /// <param name="dashboard"></param>
        /// <param name="webUrl"></param>
        /// <param name="deploymentPath"></param>
        /// <returns></returns>
        public Uri Deploy(Dashboard dashboard, string webUrl, string deploymentPath, string masterPagePath)
        {
            // The dashboard could have an old deployment path or missing paths.  Importer
            // clears dashboard deployment paths.
            dashboard.DeploymentPath = deploymentPath;
            dashboard.MasterPagePath = masterPagePath;

            string listUrl = "/Lists/PerformancePoint Content/";
            FirstClassElementCollection fcoColl = PerformancePointSettings.Current.GetAuthoringService().GetListItems(listUrl);

            // Get an instance of ScorecardModel and populate
            // it with the repository items.
            // ScorecardModel throws a NullReferenceException
            // if it does not have all the items it needs to
            // generate the dashboard mark-up.

            foreach (FirstClassElement element in fcoColl)
            {
                ScorecardModel.GetInstance().ElementsOnServer.AddElement(element);
            }


            // copied from Microsoft.PerformancePoint.Scorecards.DesignerPlugins.Dialogs.DeployForm
            SharePointDeploy sharePointDeploy =
                new SharePointDeploy(
                    SharePointProxy.CreateSharePointProxy(new Uri(webUrl)),
                    dashboard,
                    string.IsNullOrEmpty(dashboard.PageList) ? (0 < dashboard.Pages.Count) : Convert.ToBoolean(dashboard.PageList, CultureInfo.CurrentUICulture));

            // ScorecardModel.GetInstance().ElementsOnServer needs to be refreshed with
            // any elements on the dashboard before DeployDashboard is called.             
            return sharePointDeploy.DeployDashboard(dashboard.DeploymentPath, dashboard.MasterPagePath);
        }        
    }
}